About this Article Article

More Articles about Oxygene

CH01 - Introducing the Oxygene Command...

CH02 - ASP.NET Scripting with Oxygene

CH03 - Generics in Oxygene

CH04 - Introduction to Oxygene

CH05 - Your First Oxygene Project

CH06 - Creating Custom File and Project...

CH07 - Creating Cocoa# applications for...

CLG09 - Sequences and Queries

DA35 - Building a Data Abstract Client...

WP02 - Cross Platform Development with...

Articles for this product are grouped in:

First Steps

Interoperability

Data Access

Advanced

Oxygene Language Guide

Whitepaper

Search Articles


Advanced Search...

CH02 - ASP.NET Scripting with Oxygene

This article will show you how to make use of Oxygene as a scripting language inside your ASP.NET applications.

In general, ASP support for a given language comes in two parts. First, you can use the language inside Visual Studio to design your WebForms, create general purpose classes and code-behind (or in 2.0 code-beside) files. Second, you can use inline scripting code inside your .aspx files to control output, much as you did with traditional ASP or other web technologies such as PHP.

You can chose languages for these two parts separately, so you can mix a Oxygene web project with C# or JavaScript inline code, or use Oxygene inline scripting code in your C#, Visual Basic (or Oxygene) web project. This article focuses on the latter scripting support.

Setting up Shop

The Oxygene installer registers the Oxygene language provider with ASP and tells the execution engine where to locate it when needed, so you are ready to go.

If, for any reason, you are installing Oxygene manually, you will need to tell the ASP engine about the Oxygene language and how to access it. For this, you must add a new <compiler> tag to your Web.config file, as shown below. The Web.config file is a plain-text XML file located in the base folder of your web project and can be edited with any text editor:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <compilation defaultLanguage="Oxygene" debug="true">
      <compilers>
        <compiler language="Oxygene" extension=".pas"
                  type="RemObjects.Oxygene.CodeDom.OxygeneCodeProvider,
                  RemObjects.Oxygene.CodeModel, Version=3.0.11.501,
                  Culture=neutral, PublicKeyToken=3df3cad1b7aa5098" />
        ...

ASP support is provided by a so called CodeDom Provider, which is contained in the RemObjects.Oxygene.CodeModel.dll that comes with all editions of Oxygene (including the free Command Line Edition). This dll can either be located in the Global Assembly Cache (GAC), where it is placed automatically by the Oxygene installer, or in the /bin subfolder of your website project.

When deploying the dll manually (opposed to using the Oxygene installer), make sure to also deploy the RemObjects.Oxygene.Compiler.dll, which is required by the CodeDom. This dll also can be placed in the GAC or your local /bin folder.

Note that the reference to the CodeModel .dll is strongly named and includes a version number. This will ensure that your ASP website will always run with the specific version of Oxygene that you tested it with - even if multiple (newer or older) versions of Oxygene are installed on the server system.

Your First Oxygene ASPX Page

Once this is set up, lets jump right in and create a simple .aspx page to test the Oxygene scripting engine. Create the following file and drop it in your web projects's base folder...

<%@ Page language="Oxygene" %>

<html>
  <body >
    <%='Hello from Oxygene'%>
  </body>
</html>

...and save it as Hello.aspx.

If you now browse to your test server, for example at http://localhost/Hello.aspx, you will see a plain white website, with the words "Hello from Oxygene". Not spectacular, but then - what Hello World application ever has been?

What we did was to use the <%=...%> ASP tag, which is a shorthand for <% __output.Write(...) %>, to write a simple pascal string constant as the response. Of course, since ASP.NET scripting is backed by the full-fledged Oxygene compiler, you can do far more then just that and use pretty much any Oxygene language construct inside <%...%> tags, as the following sample shows:

<%@ Page language="Oxygene" %>

<html>
  <body >
    Some Numbers:

    <% for i: integer := 0 to 10 do begin %>
      <%=i.ToString%>,
    <% end; %>

  </body>
</html>

Just as you might be familiar with ASP or other languages ASP.NET supports, you can use block and loop constructs, mix standard HTML with <%...%> scripting or enclose HTML inside begin/end blocks for loops or conditional output. Any code that you could write inside a method in a normal Oxygene project, you can also use inside scripting.

Finally, you can also use the var keyword to declare new variables when you need them. While in standard Oxygene, code variables are declared at the top of the method in a separate var section, this would not be possible in <%...%> scripting tags, so inline variable declarations are allowed, as for example:

<%@ Page language="Oxygene" %>

<% var x: DateTime := DateTime.Now; %>
Today is <%=x%>.

Script Tags

Internally, ASP represents every page as a Oxygene class, with the basic logic for rendering the page provided by ASP (and expanded by your <%...%> tags). Eventually you will find that you want to add more complex code to your page, by actually expanding the page class with methods, properties or even events that you can then use from your <%...%> tags. This is provided by <script runat="server"> tags and you can define pretty much any type of class member you might need in these tags.

Consider the following example:

<%@ Page language="Oxygene" %>

  <script runat="server">

    method CalcValue(x: integer);
    var
      lValue: integer := 5;
    begin
      for i: integer := 1 to x do
        lValue := lValue*i;
      fValue := lValue.ToString;

      if assigned(DoneCalcing) then
        DoneCalcing(self, EventArgs.Empty);
    end;

    property Value: string read fValue;
    var fValue: string := 'not calced yet';

    event DoneCalcing: EventHandler;

  </script>

<html>
  <body >
    <%=Value%>

    <% CalcValue(10); %>
    
    <%=Value%>
  </body>
</html>

Note how this (granted, very contrived) code defines methods, properties and an event that can then be accessed from the main line of code in the <%...%> tags. Also note that the defined method (CalcValue) becomes a full-fledged member of the page class and can access all the other fields and properties of the class.

Conclusion

You are all set to get started coding your website. If you're curious as to how all of this works behind the scenes, watch out for our next article (currently being prepared) that will have a quick look into what actually happens when ASP.NET runs your .aspx file.